<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="com.stone.para.IConstant"%>
<%
	String contextPath = request.getContextPath();
	String flag = (String) request.getParameter("flag");
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=IConstant.PROGRAEM_TITLE%></title>
        <link rel="stylesheet" href="<%=contextPath%>/ext3/resources/css/ext-all.css" type="text/css"   />
        <script src="<%=contextPath%>/ext3/adapter/ext/ext-base.js"></script>
        <script src="<%=contextPath%>/ext3/ext-all.js"></script>
        <script language="javascript">
            Ext.BLANK_IMAGE_URL = '<%=contextPath%>/ext3/resources/images/default/s.gif';
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = 'qtip';
            var cpath = '<%=contextPath%>';
		    var checking = false;
		    var tree;
		    var oOption;
		    
		    var ids;
		    
	String.prototype.startWith=function(str){
		if(str==null||str==""||this.length==0||str.length>this.length)
			return false;
		if(this.substr(0,str.length)==str)
			return true;
		else
			return false;
		return true;
	}
	
		   
			Ext.onReady(function(){
			
			var tb = new Ext.Toolbar({
		    renderTo: document.all.tbar,
		    items: [
 				'->',{
			        text: '确定',
			        icon:"<%=contextPath%>/images/edit.gif",
			        handler:setUser
			    }
		    ]
			});
		
			var storeSQ = new Ext.data.Store({
        		proxy:new Ext.data.HttpProxy({url:'fxgz!getListExtTree.do'}),
        		reader:new Ext.data.JsonReader({},['id','text','qtip','parentId']),
        		autoLoad:true,
        		listeners:{"load":function(store,records){
        							if (records != null && records != "") {
        							
        								var arrayoldid = window.opener.document.frmM.elements["idsalready"].value.split(",");
        								
										for (var i=0; i<records.length; i++){
										
											var temp = records[i].data.text;
											if (records[i].data.text.length > 20 ) {
												temp = records[i].data.text.substring(0,20) + "……"
											}
	       									var n = new Ext.tree.TreeNode({
                								id:records[i].data.id,
                								text:temp,
                								qtip:records[i].data.text,
                								leaf:true,
                								checked:false
											});
											
	       									// 第一层菜单时
	       									if ((records[i].data.qtip + "") == "000") {
	       										n.attributes.icon = '<%=contextPath%>/images/redlight.gif';
            									tree.root.appendChild(n);
            								// 下级菜单
	       									} else {
	       									
	       										try {
	       											n.attributes.icon = '<%=contextPath%>/images/cog.png';
	       											tree.getNodeById(records[i].data.parentId).leaf = false;
	       											tree.getNodeById(records[i].data.parentId).appendChild(n);
	       										} catch (e) {
	       										}
	       									}
   										}
   										
   						checking = true;
   										
						// 选中已有的部门
						for (var j=0; j<arrayoldid.length; j++) {
							var n = tree.getNodeById(arrayoldid[j]);

							if (n != null && n.attributes.qtip != "000") {
								n.attributes.checked = true;
								
								if(n.parentNode != null) {
									n.parentNode.ui.toggleCheck(true);
								} 
							}
						}
						
						checking = false;

						tree.expandAll();
   										
									} 
                    			}}
    		});

            
		/* 生成Ttee,放到tree Div里面 */
 		tree = new Ext.tree.TreePanel({
                        id:"mytree",   
                        renderTo : "tree",
                        rootVisible:false,
                        root : new Ext.tree.TreeNode({   
                    			id : "root", 
                    			draggable:false, 
                    			singleClickExpand:true, 
                    			text : "<%=IConstant.PROGRAEM_TITLE%>"
	 						}),  
                        store : storeSQ,   
                        width : 800,   
                        height : 600,
				        useArrows:true,
				        autoScroll:true,
				        animate:true,
				        containerScroll:true,
				        checked:false
				});
				
						// checkbox触发事件
		tree.on('checkchange', function(node,checked) { 
	
			if(checking){ 
				return false;
			}
				
			checking = true;
			// 选中的话
			if(node.ui.isChecked()){
				checkChildren(node,true);
				checkParent(node);
			
			// 没选中
			} else {
				checkChildren(node,false);
				uncheckParent(node);
			}
		
			checking = false;
		});
				
				
	// 子结点处理
	function checkChildren(node, checked)
	{
		//for(var i =0;i<node.childNodes.length;i++) {
		//	node.childNodes[i].ui.toggleCheck(checked);
		//	checkChildren(node.childNodes[i], checked);
		//}
       node.attributes.checked = checked;
       node.ui.toggleCheck(checked);
       node.eachChild(function(child){
         checkChildren(child,checked);
       }); 
	}

	// 选中所有父结点
	function checkParent(node) {
		if(node.parentNode == null){
			return false;
		}
		node.parentNode.ui.toggleCheck(true);
		checkParent(node.parentNode);
	}

	// 取消所有父结点
	function uncheckParent(node) {
		if(node.parentNode == null) {
			return false;
		}
		var blCheck = true;
		for(var i =0;i<node.parentNode.childNodes.length;i++) {
			if(node.parentNode.childNodes[i].ui.isChecked()) {
				blCheck = false;
			}
		}
		if(blCheck) {
			node.parentNode.ui.toggleCheck(false);
			uncheckParent(node.parentNode);
		}
	}
 		});
 		
 		function setUser() {
 		
 			ids = "";

 			for (var i=0;i<tree.root.childNodes.length;i++) {
    			getCheckMenuID(tree.root.childNodes[i]);
    		}
    		
			if (ids.startWith(",")) {
				ids = ids.substring(1,ids.length);
			}

			if (window.opener.document.frmM.elements["idsalready"].value == ids) {
				window.close();	
				return;
			}
 			
 			window.opener.saveFxgz(ids);
			window.close();	
 		}
 		
		// 过滤选择选中的复选框
		function getCheckMenuID(node) {

			if (node.ui.isChecked()) {
	
				if (node.attributes.qtip != "000") {
					ids += "," + node.id;
				}
    			for (var i=0;i<node.childNodes.length;i++) {
    				getCheckMenuID(node.childNodes[i]);
    			}
			}

		}

		</script>
	</head>
	<body>
		<div id="tbar" top="0px" style="text-align:right">

		<!--
			<input type="button" value="确　定 " onclick="javascript:setUser()"/>-->
		</div>
		<div id="tree" style="position:absolute;left:0px;top:25px;width:100%;height:90%;overflow:hidden;border:none">
		</div>

	</body>
</html>
